package projectwork.shchuangge.cn.com.newproject.ui.fragment;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import projectwork.shchuangge.cn.com.newproject.R;
import projectwork.shchuangge.cn.com.newproject.adapter.ShoppingAdapter;
import projectwork.shchuangge.cn.com.newproject.uitls.DensityUtil;
import projectwork.shchuangge.cn.com.newproject.uitls.Utils;

/**
 * Created by 赵航 on 2017/6/17.
 */

public class ShoppingFragment extends BeasicFragment implements View.OnClickListener {

    private RecyclerView mRecyclerView;
    private TextView mChoice;
    private PopupWindow mPopupWindow;


    @Override
    protected Object requestNetData() {

       Utils.runOnUIThread(new Runnable() {
           @Override
           public void run() {
               ShoppingAdapter adapter = new ShoppingAdapter();
               mRecyclerView.setAdapter(adapter);
           }
       });
        return " ";
    }

    @Override
    public View creatSuccessView() {
        View view = View.inflate(getContext(), R.layout.shopping_fragment, null);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.shopping_list_item);
        //设置布局格式
        LinearLayoutManager manager = new GridLayoutManager(getContext(), 2);
        mRecyclerView.setLayoutManager(manager);
        mChoice = (TextView) view.findViewById(R.id.shopping_price2);
        mChoice.setOnClickListener(this);
        //初始化弹窗
        View popupView = View.inflate(getContext(), R.layout.layout_popupwindow, null);

        if (mPopupWindow==null) {
            mPopupWindow = new PopupWindow(popupView, DensityUtil.dip2px(270) , LinearLayout.LayoutParams.MATCH_PARENT, true);

        }

        //点击其他地方消失
     mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
         @Override
         public void onDismiss() {

                 WindowManager.LayoutParams params=getActivity().getWindow().getAttributes();
                 params.alpha=1f;
                 getActivity().getWindow().setAttributes(params);

         }
     });

        return view;
    }

    private void popuwindowShown(View view) {

        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        mPopupWindow.showAtLocation(view,LinearLayout.SHOW_DIVIDER_END,0,0);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.shopping_price2:
                popuwindowShown(view);
                backgroundAlpha(0.2f);
        }
    }
    private void backgroundAlpha(float f) {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = f;
        getActivity().getWindow().setAttributes(lp);
    }
}
